Docker Compose Networking
- Source
- By default Compose sets up a single network for your app.
- Each container for a service joins the
defaultnetwork and is both reachable by other containers on that network, and discover-able by them at a host-name identical to the container name.
- Each container for a service joins the
- For example, suppose your app is in a directory called
myapp, and yourdocker-compose.ymllooks like this:
version: "3.9"
services:
web:
build: .
ports:
- "8000:8000"
db:
image: postgres
ports:
- "8001:5432"
- When you run
docker compose up, the following happens:- A network called
myapp_defaultis created. - A container is created using web’s configuration. It joins the network
myapp_defaultunder the nameweb. - A container is created using db’s configuration. It joins the network
myapp_defaultunder the namedb.
- A network called
Each container can now look up the host-name
webordband get back the appropriate container’s IP address. For example,web’s application code could connect to the URLpostgres://db:5432and start using thePostgresdatabase.
HOST_PORT Vs CONTAINER_PORT
- For db, the
HOST_PORTis 8001 and the container port is5432(postgresdefault).- Outside applications use
HOST_PORTand inside(inside container) applications useCONTAINER_PORT
- Outside applications use
- Networked service-to-service communication uses the
CONTAINER_PORT. WhenHOST_PORTis defined, the service is accessible outside the swarm as well. - Within the
webcontainer, your connection string to db would look likepostgres://db:5432, and from the host machine, the connection string would look likepostgres://{DOCKER_IP}:8001.
Links
- Links allow you to define extra aliases by which a service is reachable from another service.
- They are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.
- In the following example,
dbis reachable from web at thehostnamesbothdbanddatabase.
version: "3.9"
services:
web:
build: .
links:
- "db:database"
db:
image: postgres
Specify Custom Networks
- Instead of just using the default app network, you can specify your own networks with the top-level
networkskey. - This lets you create more complex
topologiesand specify custom network drivers and options. - Each service can specify what
networksto connect to with the service-levelnetworkskey, which is a list of names referencing entries under the top-levelnetworkskey.
version: "3.9"
services:
proxy:
build: ./proxy
networks: # specify which custom network to connect to at service level networks key
- frontend
app:
build: ./app
networks:
- frontend
- backend
db:
image: postgres
networks:
- backend
networks: # define your custom networks at top level networks key
frontend:
backend:
- Based on the above custom network definition
- The
proxyservice is isolated from thedbservice, because they do not share a network in common - onlyappcan talk to both.
- The
More
- You can connect to external networks
- This is useful if you need to connect two docker-compose networks
- docker-networks-explained